//
//  SRSplashView.m
//  SportRing
//
//  Created by mykoma on 14-9-14.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRSplashView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "LGBaseDataSource.h"

@implementation SRSplashViewModel

@end

@implementation SRSplashView

-(void)setViewModel:(SRSplashViewModel *)viewModel
{
    _viewModel = viewModel;
    [self addView];
}

-(void)addView
{
    [self.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
     {
         [obj removeFromSuperview];
    }];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:self.bounds];
    [self addSubview:imageView];
    [self addSubview:imageView2];
    
    
    imageView2.image = [UIImage imageNamed:@"启动页1136"];
    UIImageView *bgimageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 282, 159.5)];
    bgimageView.center = CGPointMake(CGRectGetMidX(imageView.bounds), imageView.center.y - 10);
    bgimageView.image = [UIImage imageNamed:@"启动页小窗"];
    [imageView2 addSubview:bgimageView];
//    bgimageView.hidden = YES;
//    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, bgimageView.frame.size.width, 30)];
//    label1.textColor = [UIColor whiteColor];
//    label1.textAlignment = NSTextAlignmentCenter;
//    [bgimageView addSubview:label1];
//    
//    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, bgimageView.frame.size.width, 80)];
//    label2.textColor = [UIColor whiteColor];
//    label2.textAlignment = NSTextAlignmentCenter;
//    label2.numberOfLines = 0;
//    label2.font = [UIFont systemFontOfSize:12];
//    [bgimageView addSubview:label2];
    __block UIImageView *temoImageVIew1 = imageView;
    __block UIImageView *tempImageView = imageView2;
//
//    
//    
//    [LGBaseDataSource postAction:@"Common/Milepost" parameters:nil success:^(id obj) {
//        NSLog(@"%@",obj);
//        bgimageView.hidden = NO;
//    
//        NSString *account = [NSString stringWithFormat:@"%@",obj[@"data"][@"account"]];
//        NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@魔粉",account]];
//        [str1 addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Verdana-BoldItalic" size:20] range:NSMakeRange(0, account.length)];
//       [str1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(account.length, str1.length - account.length)];
//        label1.attributedText = str1;
//        
//        NSString *mileage = [NSString stringWithFormat:@"%@",obj[@"data"][@"mileage"]];
//        CGFloat mielea = [mileage floatValue] / 5000000;
//        NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"一起走过了%.1f千米",mielea]];
//        [str2 addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Verdana-BoldItalic" size:20] range:NSMakeRange(5, str2.length - 7)];
//        label2.attributedText = str2;
//        
//
    
    
    
//        [UIView animateWithDuration:1 animations:^{
//            tempImageView.alpha = 0;
//        }completion:^(BOOL finished) {
//            [tempImageView removeFromSuperview];
//        }];
    
    
//     [UIView animateWithDuration:1 delay:1 options:UIViewAnimationOptionCurveEaseIn animations:^{
//         tempImageView.alpha = 0;
//     } completion:^(BOOL finished) {
//         [tempImageView removeFromSuperview];
//        [tempImageView removeFromSuperview];
//     }];
    
    
        
    [UIView animateKeyframesWithDuration:1 delay:1 options:UIViewKeyframeAnimationOptionLayoutSubviews animations:^{
        tempImageView.alpha = 0;
    } completion:^(BOOL finished) {
        
        [NSThread sleepForTimeInterval:3];
        [tempImageView removeFromSuperview];
//        [temoImageVIew1 removeFromSuperview];
        
        
//        [UIView animateWithDuration:1 delay:10 options:UIViewAnimationOptionCurveEaseIn animations:^{
////            temoImageVIew1.alpha = 0.0f;
//        } completion:^(BOOL finished) {
//            
//        }];
        
    }];
//} failure:^(NSError *error) {
//    
//}];

    if (self.viewModel.imageUrl.length > 0) {
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.viewModel.imageUrl]];
        NSLog(@"%@",self.viewModel.imageUrl);
    }
    else
    {
        imageView.image = [UIImage imageNamed:@"loading"];
    }
    
//    [super willMoveToSuperview:newSuperview];
}


-(void)dismissSplashWithBlock:(void(^)(void))block
{
//    [UIView animateWithDuration:1 delay:2 options:UIViewAnimationOptionCurveEaseIn animations:^{
//        self.alpha = 1.0;
//    } completion:^(BOOL finished) {
//        [self removeFromSuperview];
//        
//    }];
    [UIView animateWithDuration:1
                     animations:^{
                         self.alpha = 0.0f;
                     } completion:^(BOOL finished) {
                         block();
                         [self removeFromSuperview];
                     }];
    
}

@end
